package cn.iocoder.yudao.module.reading.controller.app.community;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.common.LimitCheckRespVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.common.SensitiveCheckRespVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.report.ReportCreateReqVO;
import cn.iocoder.yudao.module.reading.service.community.CommunityLimitService;
import cn.iocoder.yudao.module.reading.service.community.CommunityReportService;
import cn.iocoder.yudao.module.reading.service.community.WxContentSecurityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 用户 APP - 社区内容安全检查 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 社区内容安全检查")
@RestController
@RequestMapping("/reading/community/security")
@Validated
@Slf4j
public class CommunitySecurityController {

    @Resource
    private WxContentSecurityService wxContentSecurityService;

    @Resource
    private CommunityLimitService limitService;

    @Resource
    private CommunityReportService reportService;

    @PostMapping("/sensitive-check")
    @Operation(summary = "检查文本内容是否包含敏感词")
    @PreAuthenticated
    public CommonResult<SensitiveCheckRespVO> checkSensitiveWords(
            @Parameter(description = "待检查的文本内容", required = true)
            @RequestBody @NotEmpty(message = "检查内容不能为空") String content) {
        
        SensitiveCheckRespVO result = wxContentSecurityService.checkTextContent(content);
        return success(result);
    }

    @PostMapping("/sensitive-check/batch")
    @Operation(summary = "批量检查文本内容是否包含敏感词")
    @PreAuthenticated
    public CommonResult<SensitiveCheckRespVO> batchCheckSensitiveWords(
            @Parameter(description = "待检查的文本内容列表", required = true)
            @RequestBody @NotEmpty(message = "检查内容不能为空") List<String> contents) {
        
        SensitiveCheckRespVO result = wxContentSecurityService.batchCheckTextContent(
                contents.toArray(new String[0]));
        return success(result);
    }

    @PostMapping("/image-check")
    @Operation(summary = "检查图片内容是否包含敏感信息")
    @PreAuthenticated
    public CommonResult<SensitiveCheckRespVO> checkImageContent(
            @Parameter(description = "图片URL", required = true)
            @RequestBody @NotEmpty(message = "图片URL不能为空") String imageUrl) {

        SensitiveCheckRespVO result = wxContentSecurityService.checkImageContent(imageUrl);
        return success(result);
    }

    @GetMapping("/post-limit-check")
    @Operation(summary = "检查发帖频控")
    @PreAuthenticated
    public CommonResult<LimitCheckRespVO> checkPostLimit() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        LimitCheckRespVO result = limitService.checkPostLimit(userId);
        return success(result);
    }

    @GetMapping("/comment-limit-check")
    @Operation(summary = "检查评论频控")
    @PreAuthenticated
    public CommonResult<LimitCheckRespVO> checkCommentLimit() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        LimitCheckRespVO result = limitService.checkCommentLimit(userId);
        return success(result);
    }

    @PostMapping("/report")
    @Operation(summary = "举报帖子或评论")
    @PreAuthenticated
    public CommonResult<Long> createReport(@Valid @RequestBody ReportCreateReqVO createReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        Long reportId = reportService.createReport(createReqVO, userId);
        return success(reportId);
    }

    @PostMapping("/reset-post-limit")
    @Operation(summary = "重置发帖频控（仅测试用）")
    @PreAuthenticated
    public CommonResult<Boolean> resetPostLimit() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        limitService.resetUserPostLimit(userId);
        return success(true);
    }

}
